<?php

class AdminOrderController extends AdminController {

	public function getIndex($type = 'customer')
	{
		$model  = new Order;
		$order = $model->orderDetail($type);
		
		$this->layout->title   = trans('order.order_list');
		$this->layout->content = View::make('admin.order.index', array('orders' => $order, 'type' => $type));
	}

	public function getList($type = 'customer')
	{
		$model  = new Order;
		$order = $model->getOrderList($type);
		
		$this->layout->title   = trans('order.order_list');
		$this->layout->content = View::make('admin.order.index', array('orders' => $order, 'type' => $type));
	}

	public function getCreate($type = 'customer')
	{
		$model  = new Customer;

		if($type == 'customer'){
			$customers = $model->customers();
		}else{
			$customers = $model->suppliers();
		}
		
		$this->layout->title = trans('order.create_order');
		$this->layout->content = View::make('admin.order.customer', array('customers' => $customers));
	}

	public function getCustomer($id)
	{
		$model  = new Customer;
		$customer = $model::find($id);

		$product_customer_model = new ProductCustomer;
		$products = $product_customer_model->selectedProducts($customer->id);
		
		$data['customer'] = $customer;
		$data['products'] = $products;

		$this->layout->title = trans('order.create_order');
		$this->layout->content = View::make('admin.order.form', $data);
	}

	public function getDept(){
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$customer = Input::get('customer', false);
			$date     = Input::get('date', false);
			$order    = Input::get('order', null);
			$dept     = 0;
			
			if($customer !== false && $date !== false){
				$date = DateFormat::format($date);

				$model = new CustomerPayment;
				$dept = $model->getDept($customer, $date, $order);
				$dept -= $model->getPayment($customer, $date, $order);
			}

			$result = array('error' => false, 'dept' => $dept);
		}

		return json_encode($result);
	}

	public function postCreate()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$order   = Input::get('order', false);
			$payment = Input::get('payment', false);
			$items   = Input::get('items', false);
			
			$model = new Order;
			$order = $model->saveData($order);

			$order_detail_model = new OrderDetail;
			$order_detail_model->deleteOldItems($order);

			$payment_model = new CustomerPayment;
			$payment_model->deleteOldPayment($order);

			if($items != false && count($items) > 0){
				foreach ($items as $key => $value) {
					$order_detail_model->saveData($value, $order->id);
				}
			}
			
			if($payment != false && count($payment) > 0){
				foreach ($payment as $key => $data) {
					$payment_model->saveData($data, $order);
				}
			}

			$result = array('error' => false, 'message' => 'saved data');
		}

		return json_encode($result);
	}

	public function getEdit($id)
	{
		$order = Order::find($id);
		$customer = Customer::find($order->customer);

		$product_customer_model = new ProductCustomer;
		$products = $product_customer_model->selectedProducts($customer->id);

		$order_detail = new OrderDetail;
		$items = $order_detail->getOrderItems($order);

		$data = array('order' => $order, 'customer' => $customer, 'products' => $products, 'items' => $items);

		$this->layout->title   = trans('order.edit_order');
		$this->layout->content = View::make('admin.order.form_edit', $data);
	}

	public function getPrint($id)
	{
		$this->layout = null;

		$model = new Order;
		$order = $model->find($id);
		$customer = Customer::find($order->customer);

		$customer_payment_model = new CustomerPayment;
		$dept = $customer_payment_model->getDept($customer->id, $order->date, $order->id);
		$dept -= $customer_payment_model->getPayment($customer->id, $order->date, $order->id);
		$totalpay = $customer_payment_model->getOrderPayment($order->id);
		$refund = $customer_payment_model->getOrderRefund($order->id);

		$order_detail = new OrderDetail;
		$items = $order_detail->getOrderItemsToPrint($order);

		$data = array('order' => $order, 'customer' => $customer, 'items' => $items, 'dept' => $dept, 'totalpay' => $totalpay, 'refund' => $refund);

		return View::make('admin.order.print', $data);
	}

	public function getOrderPayment(){
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$id    = Input::get('id', 0);
			
			$model = new CustomerPayment;
			$payment = $model->getOrderPayment($id);

			$result = array('error' => false, 'total' => $payment);
		}

		return json_encode($result);
	}

	public function getOrderRefund(){
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$id    = Input::get('id', 0);
			
			$model = new CustomerPayment;
			$refund = $model->getOrderRefund($id);

			$result = array('error' => false, 'total' => $refund);
		}

		return json_encode($result);
	}

	public function postDelete()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$id    = Input::get('id', 0);
			
			$result = array('error' => true, 'message' => 'cannot delete');

			$model = new Order;
			$order = $model->findOrFail($id);

			if($order != false){
				$order_detail_model = new OrderDetail;
				$order_detail_model->deleteOldItems($order);				

				$payment_model = new CustomerPayment;
				$payment_model->deleteOldPayment($order);

				$order->delete();

				$result = array('error' => false, 'message' => $order->id);
			}
		}

		return json_encode($result);
	}
}